<template>
  <div class="app-container">
    <el-form
      :model="Reqion"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="订单编号:" prop="purchaseOrder">
        <span style="padding-right: 55px; font-size: 15px">{{
          Reqion.purchaseOrder
        }}</span>
      </el-form-item>
      <el-form-item label="业务员:" prop="purchasePerson">
        <span style="padding-right: 55px; font-size: 15px">{{
          Reqion.purchasePerson
        }}</span>
      </el-form-item>
      <el-form-item label="供应商:" prop="supplierName">
        <span style="padding-right: 55px; font-size: 15px">{{
          Reqion.supplier.supplierName
        }}</span>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['purchasetb:purchasetb:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['purchasetb:purchasetb:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['purchasetb:purchasetb:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['purchasetb:purchasetb:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="purchasetbList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="采购订单数据"
        align="center"
        prop="purchaseTableId"
      />
      <el-table-column label="采购订单id" align="center" prop="purchaseId" />
      <el-table-column label="存货编码" align="center" prop="purchaseOrderNo" />
      <el-table-column
        label="存货名称"
        align="center"
        prop="purchaseTableName"
      />
      <el-table-column label="数量" align="center" prop="purchaseTableNum" />
      <el-table-column
        label="含税单价"
        align="center"
        prop="purchaseUnitPrice"
      />
      <el-table-column label="单价" align="center" prop="purchasePrice" />
      <el-table-column
        label="总价金额"
        align="center"
        prop="purchaseTotalPrice"
      />
      <el-table-column label="税额" align="center" prop="purchaseTaxPrice" />
      <el-table-column
        label="含税总金额"
        align="center"
        prop="purchaseUnitTotalPrice"
      />
      <el-table-column label="税率" align="center" prop="purchaseTax" />
      <el-table-column
        label="计划到货日期"
        align="center"
        prop="purchaseDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.purchaseDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['purchasetb:purchasetb:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['purchasetb:purchasetb:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购订单数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="采购订单id" prop="purchaseId">
          <el-input v-model="form.purchaseId" placeholder="请输入采购订单id" />
        </el-form-item>
        <el-form-item label="存货编码" prop="purchaseOrderNo">
          <el-input
            v-model="form.purchaseOrderNo"
            placeholder="请输入存货编码"
          />
        </el-form-item>
        <el-form-item label="存货名称" prop="purchaseTableName">
          <el-input
            v-model="form.purchaseTableName"
            placeholder="请输入存货名称"
          />
        </el-form-item>
        <el-form-item label="数量" prop="purchaseTableNum">
          <el-input v-model="form.purchaseTableNum" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="含税单价" prop="purchaseUnitPrice">
          <el-input
            v-model="form.purchaseUnitPrice"
            placeholder="请输入含税单价"
          />
        </el-form-item>
        <el-form-item label="单价" prop="purchasePrice">
          <el-input v-model="form.purchasePrice" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="总价金额" prop="purchaseTotalPrice">
          <el-input
            v-model="form.purchaseTotalPrice"
            placeholder="请输入总价金额"
          />
        </el-form-item>
        <el-form-item label="税额" prop="purchaseTaxPrice">
          <el-input v-model="form.purchaseTaxPrice" placeholder="请输入税额" />
        </el-form-item>
        <el-form-item label="含税总金额" prop="purchaseUnitTotalPrice">
          <el-input
            v-model="form.purchaseUnitTotalPrice"
            placeholder="请输入含税总金额"
          />
        </el-form-item>
        <el-form-item label="税率" prop="purchaseTax">
          <el-input v-model="form.purchaseTax" placeholder="请输入税率" />
        </el-form-item>
        <el-form-item label="计划到货日期" prop="purchaseDate">
          <el-date-picker
            clearable
            v-model="form.purchaseDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划到货日期"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  listPurchasetb,
  getPurchasetb,
  delPurchasetb,
  addPurchasetb,
  updatePurchasetb,
} from "@/api/purchase/purchasetb";
import { getPurchase } from "@/api/purchase/purchase";
export default {
  name: "Purchasetb",
  data() {
    return {
      Reqion: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购订单数据表格数据
      purchasetbList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        purchaseId: null,
        purchaseOrderNo: null,
        purchaseTableName: null,
        purchaseTableNum: null,
        purchaseUnitPrice: null,
        purchasePrice: null,
        purchaseTotalPrice: null,
        purchaseTaxPrice: null,
        purchaseUnitTotalPrice: null,
        purchaseTax: null,
        purchaseDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.queryParams.purchaseId =
      this.$route.params && this.$route.params.purchaseId;
    this.getList();
    const purchaseId = this.$route.params && this.$route.params.purchaseId;
    this.WareList(purchaseId);
  },
  methods: {
    WareList(purchaseId) {
      getPurchase(purchaseId).then((response) => {
        this.Reqion = response.data;
      });
    },
    /** 查询采购订单数据列表 */
    getList() {
      this.loading = true;
      listPurchasetb(this.queryParams).then((response) => {
        this.purchasetbList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        purchaseTableId: null,
        purchaseId: null,
        purchaseOrderNo: null,
        purchaseTableName: null,
        purchaseTableNum: null,
        purchaseUnitPrice: null,
        purchasePrice: null,
        purchaseTotalPrice: null,
        purchaseTaxPrice: null,
        purchaseUnitTotalPrice: null,
        purchaseTax: null,
        purchaseDate: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.purchaseTableId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购订单数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const purchaseTableId = row.purchaseTableId || this.ids;
      getPurchasetb(purchaseTableId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购订单数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.purchaseTableId != null) {
            updatePurchasetb(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurchasetb(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const purchaseTableIds = row.purchaseTableId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除采购订单数据编号为"' + purchaseTableIds + '"的数据项？'
        )
        .then(function () {
          return delPurchasetb(purchaseTableIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "purchasetb/purchasetb/export",
        {
          ...this.queryParams,
        },
        `purchasetb_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
  